#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This program is free software; you can redistribute it and/or modify  
# it under the terms of the GNU General Public License as published by  
# the Free Software Foundation; either version 2 of the License, or     
# (at your option) any later version.                                   
#                                                                         
# A copy of the license can be found in the license.txt file supplied   
# with this software or at: http://www.gnu.org/copyleft/gpl.html       
#

from jutebag import phpserialize

from icolumn import IColumn


class   Column(IColumn) :
    SIZE_ADDITIONAL = 160

    '''Simple Column
    '''
    def __init__(self, name=None, value='', timestamp=0, deleted=False) :
        self._name = name
        self._value = value
        self._timestamp = timestamp
        self._marked_for_delete = deleted

        self._size = 0
        ## __init__()

    def getName(self) :
        return self._name
    def setName(self, name) :
        self._name = name
    name = property(getName, setName)

    def getValue(self) :
        return self._value
    def setValue(self, value) :
        self._value = value
    value = property(getValue, setValue)

    def getTimestamp(self) :
        return self._timestamp
    def setTimestamp(self, timestamp) :
        self._timestamp = timestamp
    timestamp = property(getTimestamp, setTimestamp)

    def isMarkedForDelete(self) :
        return  self._marked_for_delete
    def setMarkedForDelete(self, setting) :
        self._marked_for_delete = setting
    markedForDelete = property(isMarkedForDelete, setMarkedForDelete)

    def getMarkedForDeleteAt(self) :
        if not self.markedForDelete :
            raise RuntimeError, 'Column "%s" is not marked for delete' % (self.name,)
        return  self.timetstamp

    def getLocalDeletionTime(self) :
        if not self.markedForDelete :
            raise RuntimeError, 'Column "%s" is not marked for delete' % (self.name,)
        return  int(self._value)
    

    def getColumnCount(self) :
        return 1
        ## getColumnCount()

    def getColumnSize(self) :
        if self._size < 1 :
            self._size = self.SIZE_ADDITIONAL + len(self._name) + len(self._value)
        return self._size
        ## getColumnSize()

    ####-----------------------------------------------------------####

    def __str__(self) :
        result = ['<Column ',]
        result.append(self.name)
        result.append(':')
        if self.isMarkedForDelete() :
            result.append('D')
        else :
            result.append('A')
        result.append('@')
        result.append(str(self.timestamp))
        result.append('>')

        return ''.join(result)
        ## __str__()

    def __eq__(self, otherColumn) :
        if not otherColumn :
            return False
        if not isinstance(otherColumn, self.__class__) :
            return False

        if self.name == otherColumn.name and self.value == otherColumn.value and self.timestamp == otherColumn.timestamp and self.markedForDelete == otherColumn.markedForDelete :
            return True
        return False
        ## __eq__()

    def comparePriority(self, otherColumn) :
        if not isinstance(otherColumn, self.__class__) :
            raise TypeError

        if self.isMarkedForDelete() :
            # tombstone always wins ties.
            if self.timestamp < otherColumn.timestamp :
                return -1
            else :
                return 1
            
        return self.timestamp - otherColumn.timestamp
        ## comparePriority()

    ####-----------------------------------------------------------####

    class   Serializer(object) :

        @staticmethod
        def serialize(column) :
            '''serialize column into stream
            '''
            return phpserialize.serialize(column)
            ## serialize()

        @staticmethod
        def deserialize(column_serialized) :
            '''deserialize column
            '''
            o = phpserialize.unserialize(column_serialized)
            return o
            ## deserialize()

        ## class Serializer()

    ## class Column


